<?php

declare(strict_types=1);

namespace app\model;

use think\model\concern\SoftDelete;

/**
 * 
 *
 * @mixin \think\Model
 * @property int $id
 * @property int $user_id
 * @property string $out_order_id
 * @property int $total_amount 价格(单位：分)
 * @property int $pay_amount 最终实际支付价格(有抵扣情况下。单位：分)
 * @property bool $pay_type 支付方式 0-其他 1-支付宝，2-微信
 * @property bool $status 状态：0未支付 1已支付
 * @property int $supplier_id 供应商id
 * @property float $supplier_settle_value 供应商结算类型数值
 * @property bool $supplier_settle_type 供应商结算类型
 * @property int $profit 代理商利润。单位：分
 * @property int $agent_id 代理商id
 * @property bool $agent_settle_type 代理商结算类型
 * @property float $agent_settle_value 代理商结算类型数值
 * @property int $agent_profit 代理商利润
 * @property int $cnid 渠道id
 * @property string $pay_time 支付时间
 * @property string $store_id 门店id
 * @property string $store_name 门店名称
 * @property string $gun_no 油枪号
 * @property string $item_name 油品
 * @property string $city_name
 * @property float $quantity 数量
 * @property int $commission 返佣金额 -1表示当月还没计算 需要实时计算
 * @property bool $settle_status 结算状态 0 未结算  1完成结算 跟服务商的计算
 * @property bool $refund_status 1已退款
 * @property string $refund_at 退款时间
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property string $deleted_at
 * @property string $commissed_at
 * @property int $platform_profit 平台利润
 * @property-read \app\model\OilStore $store
 * @property-read \app\model\BaseSupplier $supplier
 * @property-read \app\model\OilAgentChannel $channel
 * @property-read \app\model\Agent $agent
 * @property-read \app\model\DmjUsers $user
 * @method static \think\db\Query onlyTrashed()
 * @method static \think\db\Query withTrashed()
 */
class OilOrder extends CommonModel
{
    use SoftDelete;

    public $payType = [
        0 => '其他',
        1 => '支付宝',
        2 => '微信',
    ];

    public function store()
    {
        return $this->hasOne(OilStore::class, 'store_id', 'store_id');
    }

    public function supplier()
    {
        return $this->belongsTo(BaseSupplier::class, 'supplier_id');
    }

    public function channel()
    {
        return $this->belongsTo(OilAgentChannel::class, 'cnid');
    }

    public function agent()
    {
        return $this->belongsTo(Agent::class, 'agent_id');
    }

    public function user()
    {
        return $this->hasOne(DmjUsers::class, 'id', 'user_id');
    }

    public function adminList($where, $rewardRate = 0, $paginate = ['page' => 1, 'list_rows' => 15])
    {
        $list = $this->where($where)->withJoin(['channel' => function ($query) {
            $query->WithField(['channel.name' => 'channel_name']);
        }, 'agent' => function ($query) {
            $query->WithField(['username' => 'agent_name', 'company_name' => 'company_name']);
        }, 'user' => function ($query) {
            $query->WithField(['user.mobile' => 'mobile']);
        }, 'supplier' => function ($query) {
            $query->WithField(['supplier.name' => 'supplier_name']);
        }])->order('id desc')
        ->paginate($paginate);

        $statusArr = [0 => '已完成', 1 => '已退款'];
        if (!$list->isEmpty()) {
            $list->hidden(['updated_at', 'deleted_at', 'created_at', 'channel', 'agent']);
            $list = $list->toArray();
            if ($list['data']) {
                foreach ($list['data'] as $k => $v) {
                    $list['data'][$k]['status'] = $statusArr[$v['refund_status']];
                    $list['data'][$k]['pay_type'] = $this->payType[$v['pay_type']];
                    $list['data'][$k]['gun_item'] = $v['item_name'].'/'.$v['gun_no'];

                    if ('-1' == $v['commission']) {
                        if ($rewardRate > 0) {
                            $list['data'][$k]['commission'] = round($v['pay_amount'] * $rewardRate / 10000, 2); // 佣金百分比+分到元转换
                        } else {
                            $list['data'][$k]['commission'] = '0.00';
                        }
                    }
                }
            }
        } else {
            $list = $list->toArray();
        }

        return $list;
    }
}
